package com.vma.jifu.controller;

import com.jfinal.plugin.activerecord.Page;
import com.vma.jifu.bizModel.model.Express;
import com.vma.jifu.bizModel.model.Offer;
import com.vma.jifu.bizModel.model.OrderList;
import com.vma.jifu.bizModel.model.TgAward;
import com.vma.jifu.orderList.service.OrderService;
import com.vma.jifu.orderList.service.impl.OrderServiceImpl;
import com.vma.jifu.service.AgentService;
import com.vma.jifu.service.impl.AgentServiceImpl;
import com.vma.tools.KdniaoTrackQueryAPI2;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenzui on 2017/6/23.
 */
public class OrderController extends BaseController {

    private OrderService orderService = new OrderServiceImpl();

    private AgentService agentService = new AgentServiceImpl();

    public void getOrderList(){
        Integer userId = getParaToInt("userId");
        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        Integer status = getParaToInt("status");
        renderRes(orderService.qryOrderListByPage(userId,cPage,pSize,status));
    }

    public void updateOrder(){
        Integer userId = getParaToInt("userId");
        Integer orderId = getParaToInt("orderId");
        Integer status = getParaToInt("status");
        orderService.updateOrder(userId,orderId,status);
        renderRes("success");
    }

    public void getOrderDetail(){
        Integer userId = getParaToInt("userId");
        Integer orderId = getParaToInt("orderId");
        renderRes(orderService.getOrderDetail(userId,orderId));
    }


    public void commit(){
        Integer userId = getParaToInt("userId");
        Integer addressId = getParaToInt("addressId");
        Integer offerId = getParaToInt("offerId");
        Integer offerNum = getParaToInt("offerNum");
        Integer offerPrice = getParaToInt("offerPrice");
        String remark = getPara("remark");
        renderRes(orderService.commit(userId,addressId,offerId,offerNum,remark,offerPrice));
    }

    public void qryTCorders(){
        Integer userId = getParaToInt("userId");
        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        Integer status = getParaToInt("status");
        renderRes(orderService.qryFistOrderListByPage(userId,cPage,pSize,status));
    }

    public void qryJTorders(){
        Integer userId = getParaToInt("userId");
        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        Integer status = getParaToInt("status");
        renderRes(orderService.qryJTOrderListByPage(userId,cPage,pSize,status));
    }

    public void qryMyAgentOrders(){
        Integer userId = getParaToInt("userId");
        Integer agentId = getParaToInt("agentId");
        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        String start = getPara("start");
        String end = getPara("end");
        Page<OrderList> orderListPage = orderService.qryMyAgentOrders(userId,agentId,cPage,pSize,start,end);
        Map result = new HashMap();
        result.put("orderListPage",orderListPage);
        Long orderFee = 0L;
        Long award = 0L;
        if(orderListPage.getList()!=null && orderListPage.getList().size()>0){
            for(OrderList ol:orderListPage.getList()){
                orderFee+=ol.getTotalFee();
                award+=ol.getInt("award").longValue();
            }
        }
        result.put("totalOrderFee",orderFee);
        result.put("totalAward",award);

        renderRes(result);
    }

    public void qryOrderCnt(){
        Integer userId= getParaToInt("userId");
        renderRes(orderService.qryOrderCnt(userId));
    }

    public void qryAreaOrder(){
        Integer areaId = getParaToInt("areaId");
        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        Integer type = getParaToInt("type");
        renderRes(orderService.qryAreaOrder(areaId,type,cPage,pSize));
    }

    public void qryAgentAwardList(){
        Integer userId = getParaToInt("userId");
        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        Page<TgAward> awardPage = agentService.qryAgentAwardList(userId,cPage,pSize);
        Long totalCharge = agentService.qryTotalTgAward(userId);
        Map result = new HashMap();
        result.put("page",awardPage);
        result.put("totalCharge",totalCharge);
        renderRes(result);
    }
    public void EBusinessIDInfo() throws Exception {
        Integer orderId = getParaToInt("orderId");
        OrderList orderList = OrderList.dao.findById(orderId);
        String expressNumber = orderList.getExpressNumber();
        Express express = Express.dao.findById(orderList.getExpressId());
        KdniaoTrackQueryAPI2 api = new KdniaoTrackQueryAPI2();
        Map result = new HashMap();
        result.put("expressInfo",api.getOrderTracesByJson("ZTO","446614126057"));
        result.put("expressName",express.getName());
        result.put("offerImg",Offer.dao.findById(orderList.getOfferId(),orderList.getOfferVersion()).getImgUrl());
        renderRes(result);
    }
}
